const { coupon } = require("../../../../api");

// commons/diy/coupon/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    coupon_list: {
      type: Array,
      value: null
    },
    coupon_setting: {
      type: Object,
      value: {}
    },
    mch_id: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },
  ready: function () {
    this.setData({
      __wxapp_img: wx.getStorageSync('wxapp_img')
    })
    if (!this.properties.coupon_list) {
      this.getDiyInfo();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    receive: function(e) {
      if (!wx.getStorageSync('user_info') || wx.getStorageSync('user_info').length == 0 ) {
        getApp().getTopPage().setData({ // 更新父组件数据
          user_info_show: !0
        })
        return false;
      }
      var i = this,
        o = e.currentTarget.dataset.index,
        key = e.currentTarget.dataset.key;
      wx.showLoading({
        title: "领取中",
        mask: !0
      }), i.hideGetCoupon || (i.hideGetCoupon = function(t) {
        var a = t.currentTarget.dataset.url || !1;
        i.setData({
          get_coupon_list: null
        }), wx.navigateTo({
          url: a || "/pages/list/list"
        });
      }), getApp().request({
        url: getApp().api.coupon.receive,
        data: {
          id: o
        },
        success: function(t) {
          wx.hideLoading();
          if (0 == t.code) {
            var coupon_list = !i.properties.coupon_list ? i.data.coupon_list : i.properties.coupon_list;
            console.log(key,coupon_list[key]);
            coupon_list[key].is_receive = 1;
            i.setData({
              coupon_list: coupon_list
            })
          }else {
            wx.showToast({
              title: t.msg,
              duration: 2e3
            })
          }

          
          
          
          // 0 == t.code ? i.setData({
          //   get_coupon_list: t.data.list,
          //   coupon_list: t.data.coupon_list
          // }) : (wx.showToast({
          //   title: t.msg,
          //   duration: 2e3
          // }), i.setData({
          //   coupon_list: t.data.coupon_list
          // }));
        },
        complete:function(res){
          wx.hideLoading()
        }
      });
    },
    getDiyInfo: function () {
      var that = this;
      var shop = wx.getStorageSync('address_info');
      var shop_id = shop && shop.shop_id ? shop.shop_id : 0
      var item = {
        name: 'coupon',
        shop_id: shop_id,
        mch_id: that.properties.mch_id
      };
      getApp().request({
        url: getApp().api.index.get_diy_info,
        data: item,
        success: function(t) {
          that.setData({
            coupon_list : t.data.list
          })
        },
        complete: function() {
          
        }
      })
    },
    toGoods: function(t) {
      var o = t.currentTarget.dataset.goods, cat = t.currentTarget.dataset.cat, n = [],cat_arr = [];
      cat = cat ? JSON.parse(cat) : []
      for (var e in o) n.push(o[e].id);
      for (var i in cat) cat_arr.push(cat[i]);
      wx.navigateTo({
          url: "/pages/list/list?goods_id=" + n +"&cat_id=" + cat_arr,
          success: function(t) {},
          fail: function(t) {}
      });
    },
  }
})
